package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/page")
    public Result getCategoryPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = new PageResult();
        pageResult = categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping()
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.insertCategory(categoryDTO);
        return Result.success();
    }

    @PutMapping()
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.update(categoryDTO);
        return Result.success();
    }
    @PostMapping("/status/{status}")
    public Result updateCategoryStatus(@PathVariable("status") Integer status,long id) {
        categoryService.setStatus(status,id);
        return Result.success();
    }
    @DeleteMapping()
    public Result deleteCategory(long id) {
        categoryService.delete(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getCategory(int type) {
        List<Category> category = categoryService.getByType(type);
        return Result.success(category);
    }
}
